<template>
	<view class="page">
		<!-- 导航栏 -->
		<NavigationBar title="我的订单"></NavigationBar>
		<!-- 条件筛选 -->
		<FilterBar></FilterBar>
		<!-- 列表 -->
		<List v-if="list.length"></List>
		
		<!-- 暂无数据 -->
		<c-empty-data v-if="!list.length && !loadingState.default && !loadingState.default"></c-empty-data>
		<!-- loading -->
		<view style="margin-top: 20rpx;">
			<c-loading :loadingState="loadingState"></c-loading>
		</view>

		<!-- 底部标签栏 -->
		<c-customer-tabbar class="tabbar" :index="3"></c-customer-tabbar>
	</view>
</template>

<script setup>
	import { ref } from 'vue';
	import NavigationBar from "@/components/NavigationBar/index.vue";
	import FilterBar from './components/filter-bar.vue';
	import List from './components/list.vue';

	uni.hideHomeButton();

	const params = ref({
		page: 1,
		limit: 10,
	});
	const list = ref([]);
	const total = ref(0);
	const loadingState = ref({
		default: false,
		loading: false,
		stop: false,
		pullup: false,
	});
	
	loadingState.value.default = true;
	setTimeout(() => {
		list.value = [{}];
		loadingState.value.default = false;
	}, 2000);

	// 获取列表
	const getList = async () => {
		// 正在加载中并且不是第一页，或者已经加载完所有了
		if ((loadingState.value.default && params.value.page !== 1) || (loadingState.value.stop && params.value.page > 1)) return;
		loadingState.value.default = true;
		loadingState.value.pullup = false;
		loadingState.value.stop = false;

		const res = await api.order_api.getList(params.value);
		total.value = res.data.data.count;
		const list = res.data.data.list;

		if (params.value.page === 1) {
			list.value = list;
		} else {
			list.value.push(...list);
		}

		loadingState.value.default = false;
		if (list.value.length < recordTotal) {
			loadingState.value.pullup = true;
		} else if (recordTotal === 0) {
			loadingState.value.stop = false;
		} else {
			loadingState.value.stop = true;
		}

		params.value.page += 1;
	}
</script>

<style>

</style>